# -*- coding: utf-8 -*-
# @Time        :2025/3/13 10:27
# @Author      :文刀水寿
# @File        : 36_爬虫_requests_cookie登录古诗文网.py
"""
 @Description :
"""
import requests
from bs4 import BeautifulSoup
import urllib.request

# 登录页面的url
url = 'https://www.gushiwen.cn/user/login.aspx'

headers = {
    'cookie': 'Hm_lvt_9007fab6814e892d3020a64454da5a55=1741832833; HMACCOUNT=71C917125BE5E21C; Hm_lpvt_9007fab6814e892d3020a64454da5a55=1741832907',
    'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Mobile Safari/537.36 Edg/134.0.0.0',
}

response = requests.get(url=url, headers=headers)
# 获取网页源码
content = response.text

# 解析网页源码，获取__VIEWSTATE  __VIEWSTATEGENERATOR
soup = BeautifulSoup(content, 'lxml')

VIEWSTATE = soup.find('input', id='__VIEWSTATE').get('value')

VIEWSTATEGENERATOR = soup.find('input', id='__VIEWSTATEGENERATOR').get('value')

# 获取验证码图片
code = soup.find('img', id='imgCode').get('src')
code_url = 'https://www.gushiwen.cn/' + code

# 有坑 这次图片下载和之后的输入验证码是俩次请求 验证码已经改变
# urllib.request.urlretrieve(url=code_url, filename='yzm.jpg')

session = requests.session()
# 验证码的url内容
resopnse_code = session.post(code_url)
# 注意此时要用二进制数据 因为使用的是图片下载
content_code = resopnse_code.content
# wb 的模式就是将二进制数据写入到文件
with open('yzm.jpg', 'wb') as fp:
    fp.write(content_code)

code_name = input("输入验证码")
# 点击登录

post_url = 'https://www.gushiwen.cn/user/login.aspx'

data_post = {
    '__VIEWSTATE': VIEWSTATE,
    '__VIEWSTATEGENERATOR': VIEWSTATEGENERATOR,
    'from': '',
    'email': '18700222185@163.com',
    'pwd': 'liutao.123',
    'code': code_name,
    'denglu': '登录'
}

response_post = session.post(url=url, headers=headers, data=data_post)

content_post = response_post.text

with open("gushiwen.html", 'w', encoding='utf8') as fp:
    fp.write(content_post)
